package com.hwatsang.admin.action;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.hwatsang.admin.view.ProductModel;
import com.hwatsang.model.Product;
import com.hwatsang.service.ProductService;
import com.hwatsang.service.impl.FileService;
import com.opensymphony.xwork2.ActionSupport;

public class ProductAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	private static final Logger LOGGER = LoggerFactory.getLogger(ProductAction.class);	
	private final int PAGE_SIZE = 30;
	
	private String resultCode;
	private String message;
	
	private String prodPictureFileName;
	private String prodPictureContentType;
	private File prodPicture;
	private ProductModel model;
	private List<ProductModel> products;
	
	@Autowired
	private ProductService productService;
	@Autowired
	private FileService fileService;
	
	public String execute() {
		return SUCCESS;
	}
	
	public String save() {
		
		LOGGER.debug("Creating a product with name {}.", model.getName());

		Product product = new Product();
		BeanUtils.copyProperties(model, product);		
		String productId = productService.createProduct(product);
		
		if(prodPicture != null) {
			String productImgPath = null;
			try {
				productImgPath = fileService.storeProductImg(productId, prodPicture, prodPictureFileName);
			} catch (IOException e) {
				LOGGER.error(e.getMessage(), e);
			}
			
			if(StringUtils.isNotBlank(productImgPath)) {
				productService.updateProductImgPath(productId, productImgPath);
			}
		}
		
		resultCode = "1";
		return "submit_success";		
	}
	
	public String update() {
		
		LOGGER.debug("Going to update product {}.", model.getName());
		
		Product product = new Product();
		BeanUtils.copyProperties(model, product);		
		productService.updateProduct(product);
		
		return "submit_success";
	}
	
	public String list() {
		
		LOGGER.debug("Listing all of the products.");
		
		List<Product> products = productService.getAllProducts(0, PAGE_SIZE);
		this.products = new ArrayList<ProductModel>();
		for(Product product : products) {
			ProductModel pModel = new ProductModel();
			BeanUtils.copyProperties(product, pModel);
			this.products.add(pModel);
		}
		
		return "list_success";
	}

	public void setProdPicture(File prodPicture) {
		this.prodPicture = prodPicture;
	}

	public void setProdPictureFileName(String prodPictureFileName) {
		this.prodPictureFileName = prodPictureFileName;
	}

	public String getProdPictureContentType() {
		return prodPictureContentType;
	}

	public void setProdPictureContentType(String prodPictureContentType) {
		this.prodPictureContentType = prodPictureContentType;
	}
	
	public ProductModel getModel() {
		return this.model;
	}
	
	public void setModel(ProductModel model) {
		this.model = model;
	}

	public String getResultCode() {
		return resultCode;
	}

	public void setResultCode(String resultCode) {
		this.resultCode = resultCode;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
}
